package com.uchiha.nova.mainService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.uchiha.nova.core.model.dto.RoleDTO;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.model.entity.RoleBtn;
import com.uchiha.nova.core.model.entity.RoleEntity;
import com.uchiha.nova.core.model.entity.RoleNavMenu;
import com.uchiha.nova.core.model.entity.RoleServiceEntity;

import com.uchiha.nova.core.model.vo.RoleVO;
import com.uchiha.nova.core.model.query.RoleQuery;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.mapper.UserRoleMapper;
import com.uchiha.nova.mainService.service.IRoleBtnService;
import com.uchiha.nova.mainService.service.IRoleNavMenuService;
import com.uchiha.nova.mainService.service.IRoleService;
import com.uchiha.nova.mainService.service.RoleServiceService;
import com.uchiha.nova.mainService.wrapper.RoleWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色管理 控制器
 *
 * @author magic
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Tag(name = "角色管理")
@Slf4j
public class RoleController extends BaseController {

    @Resource
    IRoleService roleService;
    @Resource
    UserRoleMapper userRoleMapper;
    @Resource
    RoleServiceService roleServiceService;

    @Resource
    IRoleNavMenuService roleNavMenuService;
    @Resource
    IRoleBtnService roleBtnService;

    /**
     * 详情
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_DETAIL)
    @Operation(summary = "详情")
    public R<RoleVO> detail(@RequestBody RoleEntity roleEntity) {
        RoleEntity detail = roleService.getOne(Condition.getQueryWrapper(roleEntity));
        return R.data(RoleWrapper.build().entityVO(detail));
    }

    /**
     * 分页 代码自定义代号
     */
    @GetMapping(NovaCRUDMethodConstant.NOVA_LIST)
    @Operation(summary = "分页查询")
    public R getPage(RoleQuery query, QueryContext queryContext) {
        IPage<RoleEntity> pages = roleService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext, RoleEntity.class));
        return R.data(RoleWrapper.build().pageVO(pages));
    }

    /**
     * 新增 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_SAVE)
    @Operation(summary = "新增")
    public R save(@RequestBody RoleEntity roleEntity) {
        return R.status(roleService.save(roleEntity));
    }

    /**
     * 修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_UPDATE)
    @Operation(summary = "修改")
    public R update(@RequestBody RoleEntity roleEntity) {
        return R.status(roleService.updateById(roleEntity));
    }

    /**
     * 新增或修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_SAVEORUPDATE)
    @Operation(summary = "新增或修改")
    public R saveOrUpdate(@RequestBody RoleEntity roleEntity) {
        return R.status(roleService.saveOrUpdate(roleEntity));
    }


    /**
     * 删除 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_REMOVE)
    @Operation(summary = "逻辑删除")
    public R remove(@RequestParam String ids) {
        return R.status(roleService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 物理删除，不可恢复
     */
    @Operation(summary = "物理删除")
    @DeleteMapping(NovaCRUDMethodConstant.NOVA_DELETE)
    public R delete(@RequestParam String ids) {
        Func.toLongList(ids).forEach(item -> {

            userRoleMapper.deletePhysicByRoleId(item);
            roleServiceService.remove(new QueryWrapper<RoleServiceEntity>().eq("role_id", item));
            roleNavMenuService.remove(new QueryWrapper<RoleNavMenu>().eq("role_id", item));
            roleBtnService.remove(new QueryWrapper<RoleBtn>().eq("role_id", item));
        });
        roleService.deletePhysics(Func.toLongList(ids));
        return R.success("角色删除成功");
    }


    @PostMapping("/getList")
    public R getList(@RequestBody RoleDTO roleDTO){
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleDTO, roleEntity);
        List<RoleEntity> entities = roleService.list(Condition.getQueryWrapper(roleEntity));
        return R.data(RoleWrapper.build().listVO(entities));
    }
}
